package com.berwin.utility;


import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;

public class ClipboardUtility {

    public static void copy(Context context, String content) {
        ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        cm.setPrimaryClip(ClipData.newPlainText(null, content));
    }

    public static String get(Context context) {
        ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        if (cm != null) {
            ClipData data = cm.getPrimaryClip();
            if (data != null) {
                ClipData.Item item = data.getItemAt(0);
                if (item != null) {
                    //TODO item.getText()部分手机可能会在剪切板没有相关的文本内容返回null
                    return item.getText().toString();
                }
            }
        }
        return "";
    }
}
